#!/bin/bash
# hooks/build
# https://docs.docker.com/docker-cloud/builds/advanced/

## $IMAGE_NAME var is injected into the build so the tag is correct.
echo "[***] Build hook starting..."

echo "[---] DOCKERFILE_PATH:        ${DOCKERFILE_PATH}"
echo "[---] DOCKER_REPO:            ${DOCKER_REPO}"
echo "[---] IMAGE_NAME:             ${IMAGE_NAME}"

# $(echo "index.docker.io/user/respository" | cut -d '/' -f 3) = "repository"
if [ -z "$APPLICATION" ]; then
    APPLICATION=$(echo "${DOCKER_REPO}" | cut -d '/' -f 3)
fi

echo "[---] APPLICATION:            ${APPLICATION}"

# $(echo "index.docker.io/user/respository" | cut -d '/' -f 2-3) = "user/repository"
if [ -z "$GITHUB_USERREPO" ]; then
    GITHUB_USERREPO=$(echo "${DOCKER_REPO}" | cut -d '/' -f 2-3)
fi

echo "[---] GITHUB_USERREPO:        ${GITHUB_USERREPO}"

# Set description from github
if [ -z "$DESCRIPTION" ]; then
    DESCRIPTION=$(curl -s https://api.github.com/repos/${GITHUB_USERREPO} \
        | grep '"description".*' \
        | head -n 1 \
        | cut -d '"' -f 4)
fi

echo "[---] DESCRIPTION:            ${DESCRIPTION}"

# Find the build path (permits Dockerfile to be in a sub-directory)
BUILD_PATH=$(echo ${DOCKERFILE_PATH}| sed 's#/#..#g' | sed 's#[a-zA-Z0-9]\+#/#g' | cut -c 2- )

echo "[---] BUILD_PATH:             ${BUILD_PATH}"

# Compiler setting
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)clang3.5?(\-*))
        CC=clang-3.5
        CXX=clang++-3.5
        ;;
    ?(*\-)clang3.6?(\-*))
        CC=clang-3.6
        CXX=clang++-3.6
        ;;
    ?(*\-)clang3.7?(\-*))
        CC=clang-3.7
        CXX=clang++-3.7
        ;;
    ?(*\-)clang3.8?(\-*))
        CC=clang-3.8
        CXX=clang++-3.8
        ;;
    ?(*\-)clang3.9?(\-*))
        CC=clang-3.9
        CXX=clang++-3.9
        ;;
    ?(*\-)clang4.0?(\-*))
        CC=clang-4.0
        CXX=clang++-4.0
        ;;
    ?(*\-)clang5.0?(\-*))
        CC=clang-5.0
        CXX=clang++-5.0
        ;;
    ?(*\-)clang6.0?(\-*))
        CC=clang-6.0
        CXX=clang++-6.0
        ;;
    ?(*\-)clang7?(\-*))
        CC=clang-7
        CXX=clang++-7
        ;;
    ?(*\-)clang8?(\-*))
        CC=clang-8
        CXX=clang++-8
        ;;
    # ?(*\-)clang9?(\-*))
    #     CC=clang-9
    #     CXX=clang++-9
    #     ;;
    # ?(*\-)clang10?(\-*))
    #     CC=clang-10
    #     CXX=clang++-10
    #     ;;
    # ?(*\-)clang11?(\-*))
    #     CC=clang-11
    #     CXX=clang++-11
    #     ;;
    # ?(*\-)clang12?(\-*))
    #     CC=clang-12
    #     CXX=clang++-12
    #     ;;
    ?(*\-)gcc4.7?(\-*))
        CC=gcc-4.7
        CXX=g++-4.7
        FC=gfortran-4.7
        ;;
    ?(*\-)gcc4.8?(\-*))
        CC=gcc-4.8
        CXX=g++-4.8
        FC=gfortran-4.8
        ;;
    ?(*\-)gcc4.9?(\-*))
        CC=gcc-4.9
        CXX=g++-4.9
        FC=gfortran-4.9
        ;;
    ?(*\-)gcc5?(\-*))
        CC=gcc-5
        CXX=g++-5
        FC=gfortran-5
        ;;
    # ?(*\-)gcc6?(\-*))
    #     CC=gcc-6
    #     CXX=g++-6
    #     FC=gfortran-6
    #     ;;    
    # ?(*\-)gcc7?(\-*))
    #     CC=gcc-7
    #     CXX=g++-7
    #     FC=gfortran-7
    #     ;;
    # ?(*\-)gcc8?(\-*))
    #     CC=gcc-8
    #     CXX=g++-8
    #     FC=gfortran-8
    #     ;;
    # ?(*\-)gcc9?(\-*))
    #     CC=gcc-9
    #     CXX=g++-9
    #     FC=gfortran-9
    #     ;;
    # ?(*\-)gcc10?(\-*))
    #     CC=gcc-10
    #     CXX=g++-10
    #     FC=gfortran-10
    #     ;;
    *)
        echo "[***] Unsupported compiler"
        exit 1
        ;;
esac
shopt -u extglob;

echo "[---] CC:                     ${CC}"
echo "[---] CXX:                    ${CXX}"
echo "[---] FC:                     ${FC}"

# C++ standard
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)cxx11?(\-*))
        CMAKE_CXX_STANDARD=11
        ;;
    ?(*\-)cxx14?(\-*))
        CMAKE_CXX_STANDARD=14
        ;;
    ?(*\-)cxx17?(\-*))
        CMAKE_CXX_STANDARD=17
        ;;
    ?(*\-)cxx20?(\-*))
        CMAKE_CXX_STANDARD=20
        ;;
    ?(*\-)cxx98?(\-*))
        CMAKE_CXX_STANDARD=98
        ;;
    *)
        echo "[***] Unsupported C++ standard"
        exit 1
        ;;
esac
shopt -u extglob;

echo "[---] CMAKE_CXX_STANDARD:     ${CMAKE_CXX_STANDARD}"

# Build type
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)release?(\-*))
        CMAKE_BUILD_TYPE=Release
        ;;
    ?(*\-)debug?(\-*))
        CMAKE_BUILD_TYPE=Debug
        ;;
    ?(*\-)debinfo?(\-*))
        CMAKE_BUILD_TYPE=RelWithDebInfo
        ;;
    ?(*\-)minsize?(\-*))
        CMAKE_BUILD_TYPE=MinSizeRel
        ;;
    *)
        echo "[***] Unsupported build type"
        exit
        ;;
esac
shopt -u extglob;

echo "[---] CMAKE_BUILD_TYPE:       ${CMAKE_BUILD_TYPE}"

# Target architecture
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)auto?(\-*))
        TARGET_ARCHITECTURE="auto"
        ;;
    ?(*\-)none?(\-*))
        TARGET_ARCHITECTURE="none"
        ;;
    ?(*\-)generic?(\-*))
        TARGET_ARCHITECTURE="generic"
        ;;
    ?(*\-)core?(\-*))
        TARGET_ARCHITECTURE="core"
        ;;
    ?(*\-)merom?(\-*))
        TARGET_ARCHITECTURE="merom"
        ;;
    ?(*\-)penryn?(\-*))
        TARGET_ARCHITECTURE="penryn"
        ;;
    ?(*\-)nehalem?(\-*))
        TARGET_ARCHITECTURE="nehalem"
        ;;
    ?(*\-)westmere?(\-*))
        TARGET_ARCHITECTURE="westmere"
        ;;
    ?(*\-)sandybridge?(\-*))
        TARGET_ARCHITECTURE="sandybridge"
        ;;
    ?(*\-)ivybridge?(\-*))
        TARGET_ARCHITECTURE="ivybridge"
        ;;
    ?(*\-)haswell?(\-*))
        TARGET_ARCHITECTURE="haswell"
        ;;
    ?(*\-)broadwell?(\-*))
        TARGET_ARCHITECTURE="broadwell"
        ;;
    ?(*\-)skylake?(\-*))
        TARGET_ARCHITECTURE="skylake"
        ;;
    ?(*\-)skylake-xeon?(\-*))
        TARGET_ARCHITECTURE="skylake-xeon"
        ;;
    ?(*\-)kabylake?(\-*))
        TARGET_ARCHITECTURE="kabylake"
        ;;
    ?(*\-)cannonlake?(\-*))
        TARGET_ARCHITECTURE="cannonlake"
        ;;
    ?(*\-)cascadelake?(\-*))
        TARGET_ARCHITECTURE="cascadelake"
        ;;
    ?(*\-)cooperlake?(\-*))
        TARGET_ARCHITECTURE="cooperlake"
        ;;
    ?(*\-)icelake?(\-*))
        TARGET_ARCHITECTURE="icelake"
        ;;
    ?(*\-)icelake-xeon?(\-*))
        TARGET_ARCHITECTURE="icelake-xeon"
        ;;
    ?(*\-)tigerlake?(\-*))
        TARGET_ARCHITECTURE="tigerlake"
        ;;
    ?(*\-)alderlake?(\-*))
        TARGET_ARCHITECTURE="alderlake"
        ;;
    ?(*\-)sapphirerapids?(\-*))
        TARGET_ARCHITECTURE="sapphirerapids"
        ;;
    ?(*\-)bonnell?(\-*))
        TARGET_ARCHITECTURE="bonnell"
        ;;
    ?(*\-)silvermont?(\-*))
        TARGET_ARCHITECTURE="silvermont"
        ;;
    ?(*\-)goldmont?(\-*))
        TARGET_ARCHITECTURE="goldmont"
        ;;
    ?(*\-)goldmont-plus?(\-*))
        TARGET_ARCHITECTURE="goldmont-plus"
        ;;
    ?(*\-)tremont?(\-*))
        TARGET_ARCHITECTURE="tremont"
        ;;
    ?(*\-)knl?(\-*))
        TARGET_ARCHITECTURE="knl"
        ;;
    ?(*\-)knm?(\-*))
        TARGET_ARCHITECTURE="knm"
        ;;
    ?(*\-)atom?(\-*))
        TARGET_ARCHITECTURE="atom"
        ;;
    ?(*\-)k8?(\-*))
        TARGET_ARCHITECTURE="k8"
        ;;
    ?(*\-)k8-sse3?(\-*))
        TARGET_ARCHITECTURE="k8-sse3"
        ;;
    ?(*\-)barcelona?(\-*))
        TARGET_ARCHITECTURE="barcelona"
        ;;
    ?(*\-)istanbul?(\-*))
        TARGET_ARCHITECTURE="istanbul"
        ;;
    ?(*\-)magny-cours?(\-*))
        TARGET_ARCHITECTURE="magny-cours"
        ;;
    ?(*\-)bulldozer?(\-*))
        TARGET_ARCHITECTURE="bulldozer"
        ;;
    ?(*\-)interlagos?(\-*))
        TARGET_ARCHITECTURE="interlagos"
        ;;
    ?(*\-)piledriver?(\-*))
        TARGET_ARCHITECTURE="piledriver"
        ;;
    ?(*\-)steamroller?(\-*))
        TARGET_ARCHITECTURE="steamroller"
        ;;
    ?(*\-)excavator?(\-*))
        TARGET_ARCHITECTURE="excavator"
        ;;
    ?(*\-)amd10h?(\-*))
        TARGET_ARCHITECTURE="k8"
        ;;
    ?(*\-)amd11h?(\-*))
        TARGET_ARCHITECTURE="k8"
        ;;
    ?(*\-)amd12h?(\-*))
        TARGET_ARCHITECTURE="k8"
        ;;
    ?(*\-)amd14h?(\-*))
        TARGET_ARCHITECTURE="amd14h"
        ;;
    ?(*\-)amd15h?(\-*))
        TARGET_ARCHITECTURE="bulldozer"
        ;;
    ?(*\-)amd16h?(\-*))
        TARGET_ARCHITECTURE="amd16h"
        ;;
    ?(*\-)zen3?(\-*))
        TARGET_ARCHITECTURE="zen3"
        ;;
    ?(*\-)zen2?(\-*))
        TARGET_ARCHITECTURE="zen2"
        ;;
    ?(*\-)zen?(\-*))
        TARGET_ARCHITECTURE="zen"
        ;;
    *)
        TARGET_ARCHITECTURE="generic"
        ;;
esac
shopt -u extglob;

echo "[---] TARGET_ARCHITECTURE:    ${TARGET_ARCHITECTURE}"

# Build G+Smo with default numeric coefficient type
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)float?(\-*))
        GISMO_COEFF_TYPE="float"
        ;;
    ?(*\-)double?(\-*))
        GISMO_COEFF_TYPE="double"
        ;;
    ?(*\-)longdouble?(\-*))
        GISMO_COEFF_TYPE="long double"
        ;;
    ?(*\-)mpreal?(\-*))
        GISMO_COEFF_TYPE="mpfr::mpreal"
        ;;
    ?(*\-)mpq_class?(\-*))
        GISMO_COEFF_TYPE="mpq_class"
        ;;
    ?(*\-)posit_2_0?(\-*))
        GISMO_COEFF_TYPE="posit_2_0"
        ;;
    ?(*\-)posit_3_0?(\-*))
        GISMO_COEFF_TYPE="posit_3_0"
        ;;
    ?(*\-)posit_3_1?(\-*))
        GISMO_COEFF_TYPE="posit_3_1"
        ;;
    ?(*\-)posit_4_0?(\-*))
        GISMO_COEFF_TYPE="posit_4_0"
        ;;
    ?(*\-)posit_4_1?(\-*))
        GISMO_COEFF_TYPE="posit_4_1"
        ;;
    ?(*\-)posit_8_0?(\-*))
        GISMO_COEFF_TYPE="posit_8_0"
        ;;
    ?(*\-)posit_8_1?(\-*))
        GISMO_COEFF_TYPE="posit_8_1"
        ;;
    ?(*\-)posit_16_1?(\-*))
        GISMO_COEFF_TYPE="posit_16_1"
        ;;
    ?(*\-)posit_32_2?(\-*))
        GISMO_COEFF_TYPE="posit_32_2"
        ;;
    ?(*\-)posit_64_3?(\-*))
        GISMO_COEFF_TYPE="posit_64_3"
        ;;
    ?(*\-)posit_128_4?(\-*))
        GISMO_COEFF_TYPE="posit_128_4"
        ;;
    ?(*\-)posit_256_5?(\-*))
        GISMO_COEFF_TYPE="posit_256_5"
        ;;
    *)
        GISMO_COEFF_TYPE="double"
        ;;
esac
shopt -u extglob;

echo "[---] GISMO_COEFF_TYPE:       ${GISMO_COEFF_TYPE}"

# Build G+Smo with default index type
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)int?(\-*))
        GISMO_INDEX_TYPE="int"
        ;;
    ?(*\-)int32_t?(\-*))
        GISMO_INDEX_TYPE="int32_t"
        ;;
    ?(*\-)int64_t?(\-*))
        GISMO_INDEX_TYPE="int64_t"
        ;;
    ?(*\-)long?(\-*))
        GISMO_INDEX_TYPE="long"
        ;;
    ?(*\-)longlong?(\-*))
        GISMO_INDEX_TYPE="long long"
        ;;
    *)
        GISMO_INDEX_TYPE="int"
        ;;
esac
shopt -u extglob;

echo "[---] GISMO_INDEX_TYPE:       ${GISMO_INDEX_TYPE}"

# Build G+Smo examples
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)noexamples?(\-*))
        GISMO_BUILD_EXAMPLES=OFF
        ;;
    ?(*\-)examples?(\-*))
        GISMO_BUILD_EXAMPLES=ON
        ;;
    *)
        GISMO_BUILD_EXAMPLES=ON
        ;;
esac
shopt -u extglob;

echo "[---] GISMO_BUILD_EXAMPLES:   ${GISMO_BUILD_EXAMPLES}"

# Build G+Smo as library or in header-only mode
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nolib?(\-*))
        GISMO_BUILD_LIB=OFF
        ;;
    ?(*\-)lib?(\-*))
        GISMO_BUILD_LIB=ON
        ;;
    *)
        GISMO_BUILD_LIB=ON
        ;;
esac
shopt -u extglob;

echo "[---] GISMO_BUILD_LIB:        ${GISMO_BUILD_LIB}"

# Build G+Smo in precompiled header mode
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nopch?(\-*))
        GISMO_BUILD_PCH=OFF
        ;;
    ?(*\-)pch?(\-*))
        GISMO_BUILD_PCH=ON
        ;;
    *)
        GISMO_BUILD_PCH=OFF
        ;;
esac
shopt -u extglob;

echo "[---] GISMO_BUILD_PCH:        ${GISMO_BUILD_PCH}"

# Build G+Smo unittests
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nounittests?(\-*))
        GISMO_BUILD_UNITTESTS=OFF
        ;;
    ?(*\-)unittests?(\-*))
        GISMO_BUILD_UNITTESTS=ON
        ;;
    *)
        GISMO_BUILD_UNITTESTS=OFF
        ;;
esac
shopt -u extglob;

echo "[---] GISMO_BUILD_UNITTESTS:  ${GISMO_BUILD_UNITTESTS}"

# Build G+Smo with Trilinos support (must be before checking for MPI support)
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)trilinos?(\-*))
        GISMO_WITH_TRILINOS=ON
        GISMO_WITH_MPI=ON
        ;;
    *)
        GISMO_WITH_TRILINOS=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with OpenMP support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)clang?(\-*)|?(*\-)noomp?(\-*))
        GISMO_WITH_OPENMP=OFF
        ;;
    ?(*\-)omp?(\-*))
        GISMO_WITH_OPENMP=ON
        ;;
    *)
        GISMO_WITH_OPENMP=ON
        ;;
esac
shopt -u extglob;

# Build G+Smo with MPI support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nompi?(\-*))
        GISMO_WITH_MPI=OFF
        ;;
    ?(*\-)mpi?(\-*))
        GISMO_WITH_MPI=ON
        ;;
    *)
        if [ -z "$GISMO_WITH_MPI" ]; then
            GISMO_WITH_MPI=OFF
        fi
        ;;
esac
shopt -u extglob;

# Build G+Smo with CoDiPack support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nocodipack?(\-*))
        GISMO_WITH_CODIPACK=OFF
        ;;
    ?(*\-)codipack?(\-*))
        GISMO_WITH_CODIPACK=ON
        ;;
    *)
        GISMO_WITH_CODIPACK=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with GMP support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nogmp?(\-*))
        GISMO_WITH_GMP=OFF
        ;;
    ?(*\-)gmp?(\-*))
        GISMO_WITH_GMP=ON
        ;;
    *)
        GISMO_WITH_GMP=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with IPOpt support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)noipopt?(\-*))
        GISMO_WITH_IPOPT=OFF
        ;;
    ?(*\-)ipopt?(\-*))
        GISMO_WITH_IPOPT=ON
        ;;
    *)
        GISMO_WITH_IPOPT=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with MPFR support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nompfr?(\-*))
        GISMO_WITH_MPFR=OFF
        ;;
    ?(*\-)mpfr?(\-*))
        GISMO_WITH_MPFR=ON
        ;;
    *)
        GISMO_WITH_MPFR=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with OpenNURBS support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)noonurbs?(\-*))
        GISMO_WITH_ONURBS=OFF
        ;;
    ?(*\-)onurbs?(\-*))
        GISMO_WITH_ONURBS=ON
        ;;
    *)
        GISMO_WITH_ONURBS=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with Pardiso support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nopardiso?(\-*))
        GISMO_WITH_PARDISO=OFF
        ;;
    ?(*\-)pardiso?(\-*))
        GISMO_WITH_PARDISO=ON
        ;;
    *)
        GISMO_WITH_PARDISO=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with Pastix support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nopastix?(\-*))
        GISMO_WITH_PASTIX=OFF
        ;;
    ?(*\-)pastix?(\-*))
        GISMO_WITH_PASTIX=ON
        ;;
    *)
        GISMO_WITH_PASTIX=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with PSOLID support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nopsolid?(\-*))
        GISMO_WITH_PSOLID=OFF
        ;;
    ?(*\-)psolid?(\-*))
        GISMO_WITH_PSOLID=ON
        ;;
    *)
        GISMO_WITH_PSOLID=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with Spectra support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nospectra?(\-*))
        GISMO_WITH_SPECTRA=OFF
        ;;
    ?(*\-)spectra?(\-*))
        GISMO_WITH_SPECTRA=ON
        ;;
    *)
        GISMO_WITH_SPECTRA=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with SuperLU support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nosuperlu?(\-*))
        GISMO_WITH_SUPERLU=OFF
        ;;
    ?(*\-)superlu?(\-*))
        GISMO_WITH_SUPERLU=ON
        ;;
    *)
        GISMO_WITH_SUPERLU=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with TAUCS support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)notaucs?(\-*))
        GISMO_WITH_TAUCS=OFF
        ;;
    ?(*\-)taucs?(\-*))
        GISMO_WITH_TAUCS=ON
        ;;
    *)
        GISMO_WITH_TAUCS=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with UMFPACK support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)noumfpack?(\-*))
        GISMO_WITH_UMFPACK=OFF
        ;;
    ?(*\-)umfpack?(\-*))
        GISMO_WITH_UMFPACK=ON
        ;;
    *)
        GISMO_WITH_UMFPACK=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with UNUM support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nounum?(\-*))
        GISMO_WITH_UNUM=OFF
        ;;
    ?(*\-)unum?(\-*))
        GISMO_WITH_UNUM=ON
        ;;
    *)
        GISMO_WITH_UNUM=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with OpenCascade support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)noocc?(\-*))
        GISMO_WITH_OCC=OFF
        ;;
    ?(*\-)occ?(\-*))
        GISMO_WITH_OCC=ON
        ;;
    *)
        GISMO_WITH_OCC=OFF
        ;;
esac
shopt -u extglob;

# Build G+Smo with Surface Mesh support
shopt -s extglob;
case "$IMAGE_NAME" in
    ?(*\-)nosmesh?(\-*))
        GISMO_WITH_SMESH=OFF
        ;;
    ?(*\-)smesh?(\-*))
        GISMO_WITH_SMESH=ON
        ;;
    *)
        GISMO_WITH_SMESH=OFF
        ;;
esac
shopt -u extglob;

echo "[---] GISMO_WITH_CODIPACK:    ${GISMO_WITH_CODIPACK}"
echo "[---] GISMO_WITH_GMP:         ${GISMO_WITH_GMP}"
echo "[---] GISMO_WITH_IPOPT:       ${GISMO_WITH_IPOPT}"
echo "[---] GISMO_WITH_MPFR:        ${GISMO_WITH_MPFR}"
echo "[---] GISMO_WITH_MPI:         ${GISMO_WITH_MPI}"
echo "[---] GISMO_WITH_OCC:         ${GISMO_WITH_OCC}"
echo "[---] GISMO_WITH_ONURBS:      ${GISMO_WITH_ONURBS}"
echo "[---] GISMO_WITH_OPENMP:      ${GISMO_WITH_OPENMP}"
echo "[---] GISMO_WITH_PARDISO:     ${GISMO_WITH_PARDISO}"
echo "[---] GISMO_WITH_PASTIX:      ${GISMO_WITH_PASTIX}"
echo "[---] GISMO_WITH_PSOLID:      ${GISMO_WITH_PSOLID}"
echo "[---] GISMO_WITH_SMESH:       ${GISMO_WITH_SMESH}"
echo "[---] GISMO_WITH_SPECTRA:     ${GISMO_WITH_SPECTRA}"
echo "[---] GISMO_WITH_SUPERLU:     ${GISMO_WITH_SUPERLU}"
echo "[---] GISMO_WITH_TAUCS:       ${GISMO_WITH_TAUCS}"
echo "[---] GISMO_WITH_TRILINOS:    ${GISMO_WITH_TRILINOS}"
echo "[---] GISMO_WITH_UMFPACK:     ${GISMO_WITH_UMFPACK}"
echo "[---] GISMO_WITH_UNUM:        ${GISMO_WITH_UNUM}"

if [[ "${GISMO_WITH_TRILINOS}" == "ON" && ("${GISMO_WITH_MPI}" != "ON" || -z "$FC") ]]; then
    echo "[***] Compiling G+Smo with Trilinos support enabled requires GISMO_WITH_MPI=ON and the Fortran compiler FC to be set"
    exit 1
fi

## Build the prime image at the end.
docker build \
    --file "${DOCKERFILE_PATH}" \
    --build-arg APPLICATION=${APPLICATION} \
    --build-arg BUILD_RFC3339=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \
    --build-arg COMMIT=$(git rev-parse --short HEAD) \
    --build-arg DESCRIPTION="${DESCRIPTION}" \
    --build-arg VERSION=$(git describe --tags --always) \
    --build-arg CC=${CC} \
    --build-arg CXX=${CXX} \
    --build-arg FC=${FC} \
    --build-arg CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
    --build-arg CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} \
    --build-arg GISMO_BUILD_EXAMPLES=${GISMO_BUILD_EXAMPLES} \
    --build-arg GISMO_BUILD_LIB=${GISMO_BUILD_LIB} \
    --build-arg GISMO_BUILD_PCH=${GISMO_BUILD_PCH} \
    --build-arg GISMO_BUILD_UNITTESTS=${GISMO_BUILD_UNITTESTS} \
    --build-arg GISMO_COEFF_TYPE=${GISMO_COEFF_TYPE} \
    --build-arg GISMO_INDEX_TYPE=${GISMO_INDEX_TYPE} \
    --build-arg GISMO_VERSION=${SOURCE_BRANCH} \
    --build-arg GISMO_WITH_CODIPACK=${GISMO_WITH_CODIPACK} \
    --build-arg GISMO_WITH_GMP=${GISMO_WITH_GMP} \
    --build-arg GISMO_WITH_IPOPT=${GISMO_WITH_IPOPT} \
    --build-arg GISMO_WITH_MPFR=${GISMO_WITH_MPFR} \
    --build-arg GISMO_WITH_MPI=${GISMO_WITH_MPI} \
    --build-arg GISMO_WITH_OCC=${GISMO_WITH_OCC} \
    --build-arg GISMO_WITH_ONURBS=${GISMO_WITH_ONURBS} \
    --build-arg GISMO_WITH_OPENMP=${GISMO_WITH_OPENMP} \
    --build-arg GISMO_WITH_PARDISO=${GISMO_WITH_PARDISO} \
    --build-arg GISMO_WITH_PASTIX=${GISMO_WITH_PASTIX} \
    --build-arg GISMO_WITH_PSOLID=${GISMO_WITH_PSOLID} \
    --build-arg GISMO_WITH_SMESH=${GISMO_WITH_SMESH} \
    --build-arg GISMO_WITH_SPECTRA=${GISMO_WITH_SPECTRA} \
    --build-arg GISMO_WITH_SUPERLU=${GISMO_WITH_SUPERLU} \
    --build-arg GISMO_WITH_TAUCS=${GISMO_WITH_TAUCS} \
    --build-arg GISMO_WITH_TRILINOS=${GISMO_WITH_TRILINOS} \
    --build-arg GISMO_WITH_UMFPACK=${GISMO_WITH_UMFPACK} \
    --build-arg GISMO_WITH_UNUM=${GISMO_WITH_UNUM} \
    --build-arg TARGET_ARCHITECTURE=${TARGET_ARCHITECTURE} \
    -t ${IMAGE_NAME} \
    .

## Push image
docker push ${IMAGE_NAME}

echo "[***] ...build hook complete."
